package com.zss.controller;

import com.zss.bean.Message;
import com.zss.mvc.ResponseBody;
import com.zss.service.AdminService;
import com.zss.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {

    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        //1.   接参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2. 调用service 传传参数
        boolean result = AdminService.login(username, password);
        //3.   根据结果，返回数据给ajax
        Message msg = null;
        if (result){
            msg = new Message(0,"登录成功");
            //登录时间与ip的更新
            Date date =new Date();
            //获取ip方法
            String ip = request.getRemoteAddr();
            AdminService.updateLoginTimeAndIp(username,date,ip);
        }else {
            msg = new Message(-1,"登录失败");
        }
        //4.   将数据转化为json
        String json = JSONUtil.toJSON(msg);

        //5.    将json 返回给前端

        return json;
    }
}
